package com.springboot.frame.oauth.server.config;

import com.springboot.frame.oauth.server.security.hadler.SecurityAccessDeniedHandler;
import com.springboot.frame.oauth.server.security.hadler.SecurityAuthenticationEntryPoint;
import com.springboot.frame.oauth.server.security.provider.SecurityAuthenticationProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;

import java.nio.charset.StandardCharsets;

@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityAuthenticationProvider securityAuthenticationProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(securityAuthenticationProvider);
    }

    /**
     * 设置加密方式
     * 可以自定义
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * 请求认证配置
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                // 前后端分离的情况下可以不指定
                .loginPage("/login")
                // 这里必须要写formLogin()，
                // 不然原有的UsernamePasswordAuthenticationFilter不会出现，
                // 可以通过 loginProcessingUrl 方法来指定登录接口地址
                .loginProcessingUrl("/login")
                .permitAll()
                // 登录失败的拦截
                //.failureHandler(securityAuthenticationFailureHandler)
                //.failureForwardUrl("/failure")
                // 登录成功的拦截
                //.successForwardUrl("/userinfo")
                // 不进行登录成功拦截可自行跳转到授权页
                // .successHandler(securityAuthenticationSuccessHandler)
                //.defaultSuccessUrl("/index")
                .and()
                .logout()
                .deleteCookies("JSESSIONID")
                // 注销 URL
                .logoutUrl("/logout")
                .permitAll();
        http
                // 获取登录用户的 session
                // 设置session是无状态的
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                // 无效session跳转
                .invalidSessionUrl("/login")
                // 同账号最大允许登录数
                .maximumSessions(1)
                //session过期跳转
                .expiredUrl("/login");
    }

}
